﻿using System.IO;

namespace Mydra.IsdbTools.Tests
{
    using System.Linq;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// The tests related to the ISDb protocol.
    /// </summary>
    [TestClass]
    public class IsdbProtocolTests
    {
       

        #region Additional test attributes
        // You can use the following additional attributes as you write your tests:
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        #endregion

        /// <summary>
        /// Tests the isdb response for californication subtitles.
        /// </summary>
        [TestMethod]
        public void TestIsdbResponseForCalifornicationSubtitles()
        {
            var isdbResponse = IsdbProtocol.RequestIsdbSubtitles("irrelevant", "e9d2800", "50f6447d9d7004b2");
            Assert.AreNotEqual(string.Empty, isdbResponse);
        }

        /// <summary>
        /// Tests the isdb response parsing for californication subtitles.
        /// </summary>
        [TestMethod]
        public void TestIsdbResponseParsingForCalifornicationSubtitles()
        {
            var response = Responses.CalifornicationResponse;
            string ticket;
            var isdbMovies = IsdbProtocol.ParseIsdbResponse(response, out ticket);
            Assert.AreNotEqual(string.Empty, ticket);
            Assert.IsNotNull(isdbMovies);
            Assert.AreEqual(24, isdbMovies.Count(), "The number of returned movies is not correct");

            foreach (var isdbMovie in isdbMovies)
            {
                Assert.IsNotNull(isdbMovie.Titles, "No Titles were found for the movie");
                Assert.IsTrue(isdbMovie.Titles.Count() > 0, "No Titles were found for the movie");
                Assert.IsNotNull(isdbMovie.Subtitles, "No subtitles were found for the movie");
                Assert.AreEqual(1, isdbMovie.Subtitles.Count, "No subtitles were found for the movie");
            }
        }

        /// <summary>
        /// Tests the californication subtitle file download.
        /// </summary>
        [TestMethod]
        public void TestCalifornicationSubtitleFileDownload()
        {
            string ticket;

            var isdbMovies = IsdbProtocol.GetSubtitlesPerMovie("irrelevant", 0xe9d2800, "50f6447d9d7004b2", out ticket);

            var subtitleSource = IsdbProtocol.DownloadIsdbSubtitle(isdbMovies.First().Subtitles.First().ID, ticket);
            Assert.AreEqual(49,subtitleSource.ReadByte());
        }
    }
}
